<template>
  <div>
    <headerXX :name="title"></headerXX>
    <van-tree-select
      :main-active-index.sync="activeIndex"
      height="calc(var(--real-vh, 1vh) * 100 - 55px - env(safe-area-inset-bottom) - env(safe-area-inset-top))"
      :items="items"
      :active-id="id"
      @click-nav="onClickNav"
    >
      <template #content>
        <van-pull-refresh
          class="pull-refresh"
          v-model="refreshing"
          @refresh="onRefresh"
        >
          <van-empty
            v-if="list.length == 0 && !error"
            image="search"
            description="未找到数据"
          />
          <van-empty v-if="error" image="error" description="网络错误" />
          <van-list
            :immediate-check="false"
            v-model:loading="loading"
            :finished="finished"
            :error.sync="error"
            error-text="请求失败，点击重新加载"
            finished-text="没有更多了"
            @load="onLoad"
          >
           <!-- 列表内容 -->
          <div class="ullist">
            <div class="ul" v-for="(item, index) in list" :key="index">
              <div class="li">
                <div class="li_img">
                  <van-image
                    class="img"
                    radius="4"
                    width="70"
                    height="70"
                    :src="item.goodImage"
                    mode=""
                  >
                  </van-image>
                </div>
                <div class="li_text">
                  <div class="title WZ_size_14" style="font-weight: 700">
                    {{ item.goodName }}
                  </div>
                  <div class="text_JJ WZ_size_10 example1 XLHa">
                    {{ item.goodDescription }}
                  </div>
                  <div class="li_D">
                    <div class="Price WZ_size_10" style="color: #027571">
                      {{ item.goodPriceShow }}
                    </div>
                    <div class="li_btn" @click="itemClick(item)">预约咨询</div>
                  </div>
                </div>
              </div>
              <van-divider
                :style="{
                  borderColor: '#999',
                  margin: '5px 0',
                }"
              >
              </van-divider>
            </div>
          </div>
            <template #error>
              <div class="error-tip" @click="retryLoad">
                <van-icon name="warning" />
                <span>加载失败，点击重试</span>
              </div>
            </template>
          </van-list>
        </van-pull-refresh>
      </template>
    </van-tree-select>
  </div>
</template>

<script>
import headerXX from "../../components/title.vue";
import { getCompanyCategoryList } from "@/api/realAssets/icsharedstation/getParent/index.js";
import { getDoctorList } from "@/api/realAssets/icsharedstation/JYFW/index.js";
import { setgoods } from "../../common/SPXX";
export default {
  components: { headerXX },
  data() {
    return {
      title: "家政公司",
      activeIndex: 0,
      id: 0,
      items: [],
      list: [],
      loading: false,
      finished: false,
      refreshing: false,
      error: false,
      arrayString: {},
      activeId: 0,
      XXyz: {
        siteId: JSON.parse(localStorage.getItem("siteData")).id,
        companyId: 0,
        categoryId: 0,
        pageNumber: 1,
        pageSize: 10,
      },
      categoryList:[]
    };
  },
  created() {},
  mounted() {
    this.getinformation();
  },
  methods: {
    itemClick(item) {
      item.goodManufacturer = this.categoryList[this.activeIndex].categoryName;
      let list = [];
      list.push(item);
      let li = encodeURIComponent(JSON.stringify(list).replace(/%/g, "%25"));
      //把商品信息存储本地
      setgoods(li);
      this.$router.push({
        name: "rvicsharedstation-JYFW_choseTime",
      });
      //   this.$router.push("/BJFW/choseTime");
      //   router.push("/BJFW/choseTime");
    },
    onLoad() {
      this.loading = true;
      if (this.error) return; // 如果当前是错误状态，不自动触发加载
      if (this.refreshing) {
        this.list = [];
      }
      this.XXyz.companyId = this.arrayString.id;
      this.XXyz.categoryId = this.id;
      getDoctorList(this.XXyz).then((res) => {
    if (res.resultMessage != "SUCCESS") {
      this.list = [];
      this.error = true; // 显示错误提示
      // loading.value = false;
    } else if (res.data.length == 0) {
      this.list = [];
      this.finished = true;
    } else {
      res.data = res.data.map((item) => {
        return {
          companyId: item.companyId,
          categoryId: item.categoryId,
          goodPhone: item.doctorMobile,
          id: item.id,
          goodId: item.id,
          goodName: item.doctorName,
          goodImage: item.doctorImage,
          goodSiteId: item.goodSiteId,
          goodDescription: item.doctorProficient,
          goodLocation: item.goodLocation,
          goodManufacturer: item.goodManufacturer,
          goodSpecifications: item.doctorJob,
          remark: item.doctorDescription,
          doctorMobile: item.doctorMobile, // 服务电话
          doctorName: item.doctorName, // 姓名
          btn: "预约咨询",
        };
      });
      this.list = [...this.list, ...res.data];
      // list.value = res.data;
    }
    this.loading = false;
    // 判断是否已加载完所有数据
    if (this.list.length == res.pageNum) {
      this.finished = true;
    } else {
      this.XXyz.pageNumber++;
    }
    this.refreshing = false;
  });
    },
    // 下拉刷新
    onRefresh() {
      //   list.value = [];
      this.XXyz.pageNumber = 1;
      this.XXyz.pageSize = 10;
      this.finished = false;
      this.error = false;
      this.loading = true;
      this.refreshing = true;
      this.onLoad();
    },
    // 切换左侧分类
    onClickNav(index) {
      this.activeIndex = index;
      this.id = this.items[index].id;
      this.onRefresh();
      //   console.log(items.value[index].id);
    },
    getinformation() {
      this.arrayString = JSON.parse(decodeURIComponent(this.$route.query.XX));
      this.title = this.arrayString.companyName;
      console.log({
        siteId: JSON.parse(localStorage.getItem("siteData")).id,
        companyId: this.arrayString.id,
      });
      getCompanyCategoryList({
        siteId: JSON.parse(localStorage.getItem("siteData")).id,
        companyId: this.arrayString.id,
      }).then((res) => {
        if (res.resultCode == "1" && res.resultMessage == "SUCCESS") {
          this.items = res.data;
          this.categoryList = res.data.map((item) => {
            item.goodName = item.categoryName;
            item.type = item.categoryType;
            item.goodId = item.id;
            return item;
          });
          //默认拿到第一个id
          this.id = this.items[this.activeIndex].id;
          this.items.forEach((item) => {
            item.text = item.categoryName;
          });
          this.onLoad();
        } else {
          Toast.fail("网络错误");
        }
      });
    },
  },
};
</script>
<style>
.van-sidebar-item--select:before {
  background-color: rgb(2, 117, 113) !important;
}
</style>
<style lang="less" scoped>
@import "../../static/css/index.less";
@import "../../static/css/GYcSS.less";
.pull-refresh {
  background: #fff;
}
// .van-sidebar-item--select:before{
// 	background-color:rebeccapurple !important
// }
.error-tip {
  padding: 16px;
  color: #f56c6c;
  text-align: center;
}
.error-tip .van-icon {
  margin-right: 4px;
  vertical-align: middle;
}
</style>